package cn.itcast.bos.action;


import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.utils.BaseAction;
import cn.itcast.bos.utils.SmsUtils;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CustomerAction extends BaseAction<Customer>{

	@Autowired
	@Qualifier("jmsQueueTemplate")
	private JmsTemplate jmsTemplate;
	
	
	@Action(value="customer_sendSms")
	public String sendSms() throws Exception{
		//生成短信验证码
		String randomCode=RandomStringUtils.randomNumeric(6);
		//将短信验证码保存至session 
		ServletActionContext.getRequest().getSession().setAttribute(model.getTelephone(), randomCode);
		
		System.out.println(model.getTelephone()+"======"+randomCode);
		//编辑短信内容
		final String msg="傻逼,你好,验证码为:"+randomCode;
		//创建Mq生成者;
		jmsTemplate.send("bos_msg", new MessageCreator() {
			
			@Override
			public Message createMessage(Session session) throws JMSException {
				MapMessage mapMessage = session.createMapMessage();
				mapMessage.setString("telephone",model.getTelephone());
				mapMessage.setString("msg",msg);
				return mapMessage;
			}
		});
		return NONE;
		/*
		//调用工具类发送信息
		//String result=SmsUtils.sendSmsByHTTP(model.getTelephone(),msg);
		String result="000/XXX";
		
		if(result.startsWith("000")){
			//发送成功
			return NONE;
		}else{
			throw new RuntimeException("短信发送失败;信息结果:"+result);
		}*/
		
	}
	
	//用户注册
	private String checkcode;
	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}
	@Action(value="customer_regist",results={@Result(name="success",type="redirect",location="signup-success.html"),
			@Result(name="input",type="redirect",location="signup.html")})
	public String regist(){
		//获取验证码
		String  code = (String) ServletActionContext.getRequest().getSession().getAttribute(model.getTelephone());
		//判断验证码是否正确
		System.out.println(code+"========="+checkcode);
		if(!checkcode.equals(code) || code==null){
			System.out.println("客户注册失败");
			return INPUT;
			
		}else{
			//验证码正确
			System.out.println("客户注册成功");
			//调用webService 
			WebClient.create("http://localhost:9001/crm_management/services/customerService/customer").type(MediaType.APPLICATION_JSON).post(model);
			return SUCCESS;
		}
	}
	
	
}
